package
{
	import Box2D.Common.Math.b2Vec2;
	import flash.display.BitmapData;
	import flash.display.IBitmapDrawable;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	/**
	 * Used by GameObjects for drawing based on camera location.
	 * @author Simon Baumgardt-Wellander
	 */
	public class Camera extends GameObject
	{
		/** The x coordinate in the physics world */
		private var x:Number;
		/** The y coordinate in the physics world */
		private var y:Number;
		/** The scaling factor */
		private var scale:Number;
		
		// TODO: A lot of stuff.
		
		/**
		 * Create a new camera at the world location.
		 * @param	x		World x-coordinate
		 * @param	y		World y-coordinate
		 * @param	scale	the zoom factor. Defaults to 1.0
		 */
		public function Camera(x:Number, y:Number, scale:Number = 1.0)
		{
			this.x = x;
			this.y = y;
			this.scale = scale;
		}
		
		/**
		 * Move the camera to a specific location.
		 * @param	x
		 * @param	y
		 */
		public function moveTo(x:Number, y:Number):void
		{
			this.x = x;
			this.y = y;
		}
		
		
		
		/**
		 * Convert a screen coordinate to a world coordinate.
		 * @param	screen_position	The location in pixels, on the screen
		 * @return	The world position relating to that point on the screen.
		 */
		public function getWorldPosition(screen_position:Point):b2Vec2
		{
			return new b2Vec2(x + (screen_position.x * scale), y + (screen_position.y * scale));
		}
		
		/**
		 * Converts a world position into a pixel location.
		 * @param	world_position
		 * @return	The corresponding pixel location.
		 */
		public function getScreenPosition(world_position:b2Vec2):Point
		{
			return new Point((world_position.x - x) / scale, (world_position.y - y) / scale);
		}
		
		/**
		 * Draws a sprite on the screen given a world coordinate and an angle.
		 * @param	surface		The surface to draw on
		 * @param	sprite		The sprite to draw
		 * @param	position	The world position of the sprite
		 * @param	angle		The angle to draw the sprite at
		 */
		public function draw(surface:BitmapData, sprite:IBitmapDrawable, position:b2Vec2, angle:Number = 0.0):void
		{
			var matrix:Matrix = new Matrix();
			matrix.translate(position.x - x, position.y - y);
			matrix.scale(scale);
			surface.draw(sprite, matrix);
		}
	}

}